package com.Brewing.springboot.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.Null;

import org.apache.tomcat.jni.Proc;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author  庄磊
 * @date	2018年12月21日 下午4:36:54 
 * @version 1.0
 */
public class MergeVideoMp3 {
	
	private String ffmpegEXE;
	
	public MergeVideoMp3(String ffmpegEXE) {
		super();
		this.ffmpegEXE = ffmpegEXE;
	}
	
	public void convertor(String videoInputPath,String mp3InputPath,double seconds,String videoOutputPath) throws Exception {
		//ffmpeg.exe -i winter.avi -i bgm.mp3 -t 3 -y 新的视频.mp4
		List<String> command=new ArrayList<>();
		command.add(ffmpegEXE);
		command.add("-i");
		command.add(videoInputPath);
		
		command.add("-i");
		command.add(mp3InputPath);
		
		command.add("-t");
		command.add(String.valueOf(seconds));
		
		command.add("-vcodec h264");
		 
		command.add("-y");
		command.add(videoOutputPath);
		for(String c: command) {
			System.out.print(c+" ");
		}
		ProcessBuilder builder = new ProcessBuilder(command);
		Process process = builder.start();
		InputStream errorSteam =process.getErrorStream();
		InputStreamReader inputStreamReader = new InputStreamReader(errorSteam);
		BufferedReader br = new BufferedReader(inputStreamReader);
		
		String line = "";
		while ((line=br.readLine())!=null) {
			
		}
		if(br!=null) {
			br.close();
		}
		if(inputStreamReader != null) {
			inputStreamReader.close();
		}
		if(errorSteam !=null) {
			errorSteam.close();
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MergeVideoMp3 ffmpeg=new MergeVideoMp3("C:\\ffmpeg\\bin\\ffmpeg.exe");
		try {
			ffmpeg.convertor("C:\\ffmpeg\\bin\\winter.avi","C:\\ffmpeg\\bin\\bgm.mp3",3, "C:\\ffmpeg\\bin\\通过java.mp4");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
